package com.gyj.zookeeper.ZookeeperApi;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

/**
 * Created by Gao on 2018/10/23.
 * 删除Z序节点
 * ZooKeeper类提供了删除方法来删除指定的znode。 删除 方法的签名如下：
 * delete(String path, int version)
 * path  - Znode路径。
 * version  - znode的当前版本。
 */
public class ZKDelete {
    private static ZooKeeper zk;
    private static ZKConnection conn;

    // Method to check existence of znode and its status, if znode is available.
    public static void delete(String path) throws KeeperException, InterruptedException {
        zk.delete(path, zk.exists(path, true).getVersion());
    }

    public static void main(String[] args) throws InterruptedException, KeeperException {
        String path = "/node6/node2"; //Assign path to the znode

        try {
            conn = new ZKConnection();
            zk = conn.connect("localhost");
            delete(path); //delete the node with the specified path
        } catch (Exception e) {
            System.out.println(e.getMessage()); // catches error messages
        }
    }
}
